/*
 * Created by JFormDesigner on Thu Mar 29 16:29:28 EEST 2012
 */

package tr.gov.tubitak.bilgem.esya.certselector;

import java.awt.event.*;
import tr.gov.tubitak.uekae.esya.api.common.util.bag.Pair;

import java.awt.*;
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * @author Haydar Karabas
 */
public class ECertTreePinPanel extends JPanel {
    boolean showOnlyQualifiedCerts=true;
    public ECertTreePinPanel(boolean  showOnlyQualifiedCerts) {
        initComponents();
        this.showOnlyQualifiedCerts=showOnlyQualifiedCerts;
        eReaderCertTreePanel1.setShowOnlyQualifiedCerts(showOnlyQualifiedCerts);
        initGUI();
    }

    public ECertTreePinPanel(){
        this(false);
    }


    public void setShowOnlyQualifiedCerts(boolean showOnlyQualifiedCerts) {
        this.showOnlyQualifiedCerts = showOnlyQualifiedCerts;
        eReaderCertTreePanel1.setShowOnlyQualifiedCerts(showOnlyQualifiedCerts);
    }

    private void initGUI() {
        eReaderCertTreePanel1.loadTree();
    }

    public Pair<ReaderInfo,CertInfo> getSelectionInfo()
    {
        CertInfo selectedCertInfo = null;
        ReaderInfo selectedCertReaderInfo = null;
        try
        {
            ReaderCertListTree readerCertListTree = eReaderCertTreePanel1.getReaderCertListTree();
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)readerCertListTree.getLastSelectedPathComponent();
            if(selectedNode != null)
            {
                Object selectedNodeUserObject = selectedNode.getUserObject();
                if (selectedNodeUserObject instanceof CertInfo)
                {
                    selectedCertInfo = (CertInfo)selectedNode.getUserObject();
                }

                DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode )selectedNode.getParent();
                if (parentNode != null)
                {
                    Object userObject = parentNode.getUserObject();
                    if(userObject instanceof ReaderInfo)
                    {
                        selectedCertReaderInfo =(ReaderInfo)userObject;
                    }
                }
            }
        }
        catch(ClassCastException e)
        {
        }
        return new Pair<ReaderInfo, CertInfo>(selectedCertReaderInfo,selectedCertInfo);
    }
    
    public  String getPin(){
        return ePinPad1.getPin();
    }

    public void clearFields(){
        ePinPad1.clearFields();
    }

    private void btnRefreshActionPerformed(ActionEvent e) {
        // TODO add your code here
    }



    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        eReaderCertTreePanel1 = new EReaderCertTreePanel();
        ePinPad1 = new EPinPad();

        //======== this ========
        setLayout(new GridBagLayout());
        ((GridBagLayout)getLayout()).columnWidths = new int[] {17, 0, 12, 0};
        ((GridBagLayout)getLayout()).rowHeights = new int[] {17, 135, 125, 12, 0};
        ((GridBagLayout)getLayout()).columnWeights = new double[] {0.0, 1.0, 0.0, 1.0E-4};
        ((GridBagLayout)getLayout()).rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 1.0E-4};
        add(eReaderCertTreePanel1, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));
        add(ePinPad1, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets(0, 0, 5, 5), 0, 0));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private EReaderCertTreePanel eReaderCertTreePanel1;
    private EPinPad ePinPad1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
